<template>
  <section>
    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;padding-top: 10px;">
      <el-form :inline="true">
        <el-form-item>
          <el-input v-model="selectOptions.title" placeholder="品类名称" @keyup.enter.native="doSearch"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" v-on:click="doSearch">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="success" icon="plus" style="width: 120px"  @click="addNewCate">添加品类</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <!--列表-->
    <el-table :data="goodsListCateData" style="width: 100%" v-loading="loading">
      <el-table-column prop="id" label="ID" width="100">
      </el-table-column>
      <el-table-column prop="title" label="名称">
      </el-table-column>
      <el-table-column prop="parent_title" label="上级分类" :formatter="showParentTitle">
      </el-table-column>
      <el-table-column label="品类图片" prop="pic">
        <template slot-scope="scope">
          <a :href="scope.row.list_img" target="_blank"><img :src="scope.row.list_img" alt="" style="width: 80px; height: 50px;margin-top: 2px;"></a>
        </template>
      </el-table-column>
      <el-table-column prop="sort_order" label="权重" width="100">
      </el-table-column>
      <!--<el-table-column label="分类等级" prop="_level" :formatter="goods_level">-->
      <!--</el-table-column>-->
      <el-table-column label="是否在售" prop="is_sale" :formatter="isSale">
      </el-table-column>
      <el-table-column label="是否显示" prop="is_show" :formatter="isShow">
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="small" @click="editCataDia(scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="deleteCate([scope.row.id])">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--新增新一期菜品-->
    <el-dialog title="新增品类" v-model="addCateaShow" :close-on-click-modal="false">
      <el-form :model="addCateaForm" label-width="80px" ref="addForm">
        <el-form-item label="所属分类">
          <template slot-scope="scope">
            <el-select v-model="addCateaForm.parent_id" placeholder="请选择">
              <el-option label="顶级分类" value="0">顶级分类</el-option>
              <el-option v-for="(item, index) in cateData" :key="index" :label="item._name" :value="item.id">{{ item._name }}</el-option>
            </el-select>
          </template>
        </el-form-item>
        <el-form-item label="品类名称">
          <template slot-scope="scope">
            <div style="width: 300px;display: inline-block;">
              <el-input v-model="addCateaForm.title" auto-complete="on"></el-input>
            </div>
          </template>
        </el-form-item>
        <el-form-item label="品类描述">
          <template>
            <div style="width: 200px;display: inline-block;">
              <el-input v-model="addCateaForm.description" auto-complete="on"></el-input>
            </div>
          </template>
        </el-form-item>
        <el-form-item label="列表图片">
          <template>
            <div class="p-listPic-upload">
              <el-button type="success">上传<i class="el-icon-upload el-icon--right"></i></el-button>
              <span class="upload_plant_pic"><input type="file" name="file" class="cfg_upload_pic" @change="upInputOnchange"></span>
            </div>
            <div style="width: 120px;height: 80px; background: #ffffff;overflow: hidden;margin-top: 20px" v-if="addCateaForm.list_img">
              <img :src="addCateaForm.list_img" />
            </div>
          </template>
        </el-form-item>
        <el-form-item label="排序">
          <template slot-scope="scope">
            <div style="width: 300px;display: inline-block;">
              <el-input v-model="addCateaForm.sort_order" auto-complete="on"></el-input>
            </div>
          </template>
        </el-form-item>
        <el-form-item label="是否在售">
          <el-switch v-model="addCateaForm.is_sale" on-color="#13ce66" off-color="#ff4949" on-value="1" off-value="0"></el-switch>
        </el-form-item>
        <el-form-item label="是否显示">
          <el-switch v-model="addCateaForm.is_show" on-color="#13ce66" off-color="#ff4949" on-value="1" off-value="0"></el-switch>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click.native="addCateaShow = false">取消</el-button>
        <el-button type="primary" @click.native="setCate('add')">提交</el-button>
      </div>
    </el-dialog>

    <!--编辑品类-->
    <el-dialog title="编辑品类" v-model="editCateaShow" :close-on-click-modal="false">
      <el-form :model="editCateaForm" label-width="80px">
        <el-form-item label="所属分类">
          <template slot-scope="scope">
            <el-select v-model="editCateaForm.parent_title" placeholder="请选择">
              <el-option label="顶级分类" value="0">顶级分类</el-option>
              <el-option v-for="(item, index) in cateData" :key="index" :value="item.title">{{ item._name }}</el-option>
            </el-select>
          </template>
        </el-form-item>
        <el-form-item label="品类名称">
          <template slot-scope="scope">
            <div style="width: 300px;display: inline-block;">
              <el-input v-model="editCateaForm.title" auto-complete="on"></el-input>
            </div>
          </template>
        </el-form-item>
        <el-form-item label="品类描述">
          <template slot-scope="scope">
            <div style="width: 300px;display: inline-block;">
              <el-input v-model="editCateaForm.description" auto-complete="on"></el-input>
            </div>
          </template>
        </el-form-item>
        <el-form-item label="列表图片">
          <template>
            <div class="p-listPic-upload">
              <el-button type="success">上传<i class="el-icon-upload el-icon--right"></i></el-button>
              <span class="upload_plant_pic"><input type="file" name="file" class="cfg_upload_pic" @change="upInputOnchange"></span>
            </div>
            <div style="width: 120px;height: 80px; background: #ffffff;overflow: hidden;margin-top: 20px" v-if="editCateaForm.list_img">
              <img :src="editCateaForm.list_img" />
            </div>
          </template>
        </el-form-item>
        <el-form-item label="排序">
          <template slot-scope="scope">
            <div style="width: 200px;display: inline-block;">
              <el-input v-model="editCateaForm.sort_order" auto-complete="on"></el-input>
            </div>
          </template>
        </el-form-item>
        <el-form-item label="是否在售">
          <el-switch v-model="editCateaForm.is_sale" on-color="#13ce66" off-color="#ff4949" on-value="1" off-value="0"></el-switch>
        </el-form-item>
        <el-form-item label="是否显示">
          <el-switch v-model="editCateaForm.is_show" on-color="#13ce66" off-color="#ff4949" on-value="1" off-value="0"></el-switch>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click.native="editCateaShow = false">取消</el-button>
        <el-button type="primary" @click.native="setCate('edit')">提交</el-button>
      </div>
    </el-dialog>

    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <el-pagination
        @current-change="currentPageChange"
        :page-size="30"
        layout="prev, pager, next, jumper"
        :total="total" style="float:right;" :current-page="parseInt(selectOptions.page)">
      </el-pagination>
      <span style="float: right;margin-top: 6px;margin-right: 5px">共 {{ total }} 条数据</span>
    </el-col>

  </section>
</template>
<script type="text/ecmascript-6">
  import util from '../../util'
  import NProgress from 'nprogress'
  import { apiTree } from '../../apiTree'

  export default {
    name: 'plant',
    // 局部注册组件
    data() {
      return {
        loading: false,
        cateData: [],
        goodsListCateData: [],
        selectOptions: {
          page: this.$route.query.page !== undefined ? this.$route.query.page : 1,
          title: this.$route.query.title !== undefined ? this.$route.query.title : ''
        },
        total: 0,
        addCateaShow: false,
        editCateaShow: false,
        addCateaForm: {
          parent_id: '请选择',
          title: '',
          description: '',
          list_img: '',
          is_show: '1',
          is_sale: '1',
          sort_order: 0
        },
        editCateaForm: {
          sort_order: 0,
          id: 0,
          parent_id: '',
          title: '',
          description: '',
          list_img: '',
          is_show: '1',
          is_sale: '1',
          parent_title: ''
        }
      }
    },
    computed: {
    },
    methods: {
      // 显示商品等级
      goods_level(row) {
        return row._level + '级'
      },
      isSale(row) {
        return row.is_sale === '1' ? '在售' : '下架'
      },
      isShow(row) {
        return row.is_show === '1' ? '显示' : '隐藏'
      },
      showParentTitle(row) {
        let _title = row.parent_title;
        if (util.isEmpty(row.parent_title)) {
          _title = row.title
        }
        return _title + '(' +  row.parent_id + ')';
      },
      currentPageChange(val) {
        this.selectOptions.page = val
        this.$router.push({path: this.$route.path, query: this.selectOptions})
      },
      doSearch() {
        this.$router.push({path: this.$route.path, query: this.selectOptions})
      },
      // 上传商品照片
      upInputOnchange(e) {
        var _that = this
        let file = e.target.files[0]
        let _reader = new FileReader()
        _reader.readAsDataURL(file)
        _reader.onload = function() {
          let _file = this.result
          let param = {'file': _file, 'type': 3}
          apiTree('upload/index', 'plant/upload', param, 'POST').then((res) => {
            if (res.s === 1) {
              if (_that.addCateaShow) {
                _that.addCateaForm.list_img = res.d.url;
              } else {
                _that.editCateaForm.list_img = res.d.url;
              }
            }
          });
        }
      },
      // 新增品类
      addNewCate() {
        this.addCateaShow = true;
      },
      // 编辑品类
      editCataDia(row) {
        this.editCateaShow = true;
        this.editCateaForm = {
          sort_order: row.sort_order,
          id: row.id,
          parent_id: row.parent_id,
          title: row.title,
          description: row.description,
          list_img: row.list_img,
          is_show: row.is_show,
          is_sale: row.is_sale,
          parent_title: util.isEmpty(row.parent_title) ? '顶级分类' : row.parent_title
        }
      },
      // 设置品类
      setCate(type) {
        let param = {};
        if (type === 'add') {
          param = util.cloneObj(this.addCateaForm)
        }
        if (type === 'edit') {
          param = util.cloneObj(this.editCateaForm)
        }
        if (util.isEmpty(param.list_img) || util.isEmpty(param.description) || util.isEmpty(param.title)) {
          this.$message.error('参数为空!')
          return
        }
        // 设置品类
        apiTree('goodsCate/index', 'goods/setCate', param, 'POST').then((res) => {
          if (res.s === 1) {
            this.$message({
              message: '提交成功',
              type: 'success'
            });
            this.addCateaShow = false;
            this.editCateaShow = false;
            this.init();
          } else {
            this.$message.error(res.err_str)
            return
          }
        })
      },
      deleteCate([_id]) {
        // 删除品类
        apiTree('goodsCate/index', 'goods/deleteCate', {id: _id}).then((res) => {
          if (res.s === 1) {
            this.$message({
              message: '删除成功',
              type: 'success'
            });
            this.init();
          } else {
            this.$message.error(res.err_str)
            return
          }
        })
      },
      // 获得种子列表
      init() {
        let param = this.$route.query
        if (util.isEmptyObject(param)) {
          this.selectOptions = {
            page: 1,
            title: ''
          }
        }
        this.loading = true
        NProgress.start()
        apiTree('goodsCate/index', 'goods/tag', param).then((res) => {
          if (res.s === 1) {
            this.goodsListCateData = res.d.list_data
            this.cateData = res.d.cate_data
            this.total = res.d.total_num
            this.loading = false
            NProgress.done()
          }
        })
      }
    },
    created() {
      this.init();
    },
    watch: {
      $route: 'init'
    }
  }

</script>

<style scoped lang="stylus" rel="stylesheet/stylus">
  .p-pic-old
    display inline-block
    width 500px
  .pl-pic-upload
    display inline-block
    margin-left 10px
    position relative
  .p-pic-edit
    left: 175px;
    top: -44px;
    .upload_plant_pic
      display block
      width 80px
      height 35px
      cursor pointer
      position absolute
      top 0px
      right 20px
      opacity 0
      & input[type=file]
        display block
        cursor pointer
        width 80px
        height 35px
  .p-pl-poster
    width 160px
    height 80px
    margin-bottom 20px
    margin-top 0px
    & img
      width 100%
      height 100%
  .el-form-item
    margin-bottom 10px

  .p-listPic-upload
    display inline-block
    margin-left 10px
    position relative
    cursor pointer
    .upload_plant_pic
      display block
      width 80px
      height 35px
      cursor pointer
      position absolute
      top 0px
      left 0px
      opacity 0
      & input[type=file]
        display block
        cursor pointer
        width 80px
        height 35px
</style>
